<script setup lang="ts">
import * as echarts from 'echarts'
import { onMounted, ref } from 'vue'
let ball = ref()
onMounted(() => {
  let myCharts = echarts.init(ball.value)
  let index = 0
  let data = [
    [
      [24, 40, 101, 134, 90, 230, 210, 230, 120, 230, 210, 120],
      [40, 64, 191, 324, 290, 330, 310, 213, 180, 200, 180, 79],
    ],
    [
      [23, 75, 12, 97, 21, 67, 98, 21, 43, 64, 76, 38],
      [43, 31, 65, 23, 78, 21, 82, 64, 43, 60, 19, 34],
    ],
    [
      [34, 87, 32, 76, 98, 12, 32, 87, 39, 36, 29, 36],
      [56, 43, 98, 21, 56, 87, 43, 12, 43, 54, 12, 98],
    ],
    [
      [43, 73, 62, 54, 91, 54, 84, 43, 86, 43, 54, 53],
      [32, 54, 34, 87, 32, 45, 62, 68, 93, 54, 54, 24],
    ],
  ]

  setInterval(function () {
    //1.数组下标++ 切换数据
    index++
    if (index > 3) {
      //数组最大下标为3,
      index = 0
    }
    //2.替换数据
    option.series[0].data = data[index][0]
    option.series[1].data = data[index][1]
    //3.重新渲染 echarts图表
    myCharts.setOption(option)
  }, 3000)

  const option = {
    title: {
      text: '单位:万',
      left: 30,
      // 文字颜色
      textStyle: {
        color: '#4c9bfd',
      },
    },
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      data: ['预期销售额', '实际销售额'],
      left: 'right',
      textStyle: {
        color: '#fff',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      // 文字颜色
      axisLabel: {
        color: '#4c9bfd',
      },
      data: [
        '1月',
        '2月',
        '3月',
        '4月',
        '5月',
        '6月',
        '7月',
        '8月',
        '9月',
        '10月',
        '11月',
        '12月',
      ],
      splitLine: {
        lineStyle: {
          color: '#012f4a',
        },
      },
      axisLine: {
        show: true,
      },
      axisTick: {
        show: true,
      },
    },
    yAxis: {
      /* 刻度为数据最大刻度 */
      max: 'dataMax',
      type: 'value',
      // 文字颜色
      axisLabel: {
        color: '#4c9bfd',
      },
      //修改刻度颜色
      splitLine: {
        lineStyle: {
          color: '#012f4a',
        },
      },
      axisLine: {
        show: true,
      },
      axisTick: {
        show: true,
      },
    },
    series: [
      {
        name: '预期销售额',
        type: 'line',
        stack: '总量',
        //曲线
        smooth: true,
        //圆圈大小
        symbolSize: 8,
        itemStyle: {
          color: '#00f2f1', // 线颜色
        },
        data: data[0][0],
      },
      {
        name: '实际销售额',
        type: 'line',
        stack: '总量',
        //曲线
        smooth: true,
        //圆圈大小
        symbolSize: 8,
        data: data[0][1],
        itemStyle: {
          color: '#ed3f35', // 线颜色
        },
      },
    ],
  }
  myCharts.setOption(option)
})
</script>

<template>
  <div class="tourist">
    <div>
      <p class="title">热门景区月份</p>
      <div class="img"></div>
    </div>
    <div class="ball" ref="ball"></div>
  </div>
</template>

<style scoped lang="scss">
.tourist {
  height: 340px;
  background: url('../images/dataScreen-main-lt.png') no-repeat;
  background-size: cover;
  color: white;
  position: relative;
  .title {
    font-size: 24px;
  }
  .img {
    width: 68px;
    height: 7px;
    background: url('../images/dataScreen-title.png') no-repeat;
    background-size: cover;
    margin-top: 8px;
  }
  .ball {
    position: absolute;
    bottom: 5px;
    width: 100%;
    height: 80%;
  }
}
</style>
